package Timer;
//用System.currentTimeMillis()实现获取年月日时分秒


public class Timer {
	private static int startyear = 1970;
	private static int yearday = 365;
	private static int lyearday = 366;
	
	//public static int bmouth = 31;
	//public static int smouth = 30;			//这部分分别记录了每一个月可能的天数
	private static int tmouth = 28;
	private static int ltmouth = 29;
	
	private static int count = 0; 
	private static int mcount = 1;			//这部分记录的是不同的计数器
	private static int dcount = 0;
	
	private static long time = System.currentTimeMillis();			//获得1970到现在的毫秒
	
	private static long day = 0;
	private static long year = 0;
	private static long mouth = 0;
	private static long hour = 0;
	private static long TimeDifference = 8;//这部分的变量来存储时间
	private static long localtime = 0;
	private static long minute = 0;
	private static long second = 0;
	private static long ms = 0;
	
	
	private static long dayms = 24*60*60*1000;
	private static long hourms = 60*60*1000;
	private static long minutems = 60*1000;			//这部分存储常量
	private static long secondms = 1000;
	
	private static long ttsecond = 0; 
	private static long thour = 60*60*1000;
	private static long tminute = 60*1000;			//这部分存放临时变量
	private static long tsecond = 1000;
	
	//public static void main(String[] args)
	//{
		//Data();		
		//Clock();
		//System.out.println(hour);
		//if(localtime>=24)
		//{
		//	localtime-=24;
			//System.out.println(localtime);
		//	day+=1;
		//}
		//timerStart();
		//timerEnd();
	//}
	
	private static void Clock()
	{
		thour = time%dayms;
		tminute  = thour%hourms;
		tsecond = tminute%minutems;
		ms = tsecond%secondms;
		//System.out.println(ms);
		second = (tsecond - ms)/secondms;
		ttsecond = second;
		//System.out.println(second);
		minute = (tminute - tsecond)/minutems;
		//System.out.println(minute);
		hour += (thour - tminute)/hourms;
		localtime = hour + TimeDifference;
		//if(hour>=24)
		//{
			//hour=0;
		//}
		//System.out.println(hour);
		//System.out.println(localtime);
		
	}
	private static void Data()
	{
		int k = 1970;
		int j = 0;
		day = time/dayms;
		year = (day/yearday)+startyear;
		
		while(true)
		{
			if(((k%4==0)&&(k%100!=0))||(k%400==0))	
			{
				count++;
			}
			k++;			
			if(k==year)
				break;		
		}
		//System.out.println(count);
		j = ((int)year - 1 - startyear) * yearday + count; 
		//System.out.println(j);
		dcount = (int)day - j - lyearday + 2;
		//System.out.println(dcount);
		MouthDay();
		mouth = mcount;
		day = dcount;
		
		
	}
	
	private static void MouthDay()
	{
		//int Omouth = bmouth;
		int Tmouth;
		if(((year%4==0)&&(year%100!=0))||(year%400==0))
			Tmouth = ltmouth;
		else
			Tmouth = tmouth;
		//int Thmouth = bmouth;
		//int Fmouth = smouth;
		//int Fimouth = bmouth;
		//int Smouth = smouth;
		//int Stmouth = bmouth;
		//int Emouth = bmouth;
		//int Stemmouth = smouth;
		//int Ocmouth = bmouth;
		//int Elmouth = smouth;
		//int Twmouth = bmouth;
		
	
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-Tmouth)>0)
		{
			dcount-=Tmouth;
			mcount++;
		}
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-30)>0)
		{
			dcount-=30;
			mcount++;
		}
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-30)>0)
		{
			dcount-=30;
			mcount++;
		}
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-30)>0)
		{
			dcount-=30;
			mcount++;
		}
		if((dcount-31)>0)
		{
			dcount-=31;
			mcount++;
		}
		if((dcount-30)>0)
		{
			dcount-=30;
			mcount++;
		}

	}
	
	public static void timerStart()
	{
		Data();		
		Clock();
		if(localtime>=24)
		{
			localtime-=24;
			//System.out.println(localtime);
			day+=1;
		}
		System.out.print(year+"年"+mouth+"月"+day+"日");
		System.out.print("	时间为:	");//输出年月日
		System.out.println(localtime+" : "+minute+" : "+second+" : "+ms);  //输出时间
	}
	
	public static void timerEnd()
	{
		long t = System.currentTimeMillis();
		thour = t%dayms;
		tminute  = thour%hourms;
		tsecond = tminute%minutems;
		ms = tsecond%secondms;
		//System.out.println(ms);
		second = (tsecond - ms)/secondms;
		System.out.println();
		System.out.println("程序运行时间: "+(second-ttsecond)+"s");
	}
}
